//
//  CocoaHttpServerViewController.m
//  myDemo2
//
//  Created by wuxueying on 14-4-18.
//
//

#import "CocoaHttpServerViewController.h"
#import "HTTPServer.h"
#import "DDLog.h"
#import "DDTTYLogger.h"
#import "IPDetector.h"

@interface CocoaHttpServerViewController ()
{
    HTTPServer *httpServer;
}
@property (weak, nonatomic) IBOutlet UILabel *ip;
@property (weak, nonatomic) IBOutlet UILabel *port;
@end
@implementation CocoaHttpServerViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [IPDetector getLANIPAddressWithCompletion:^(NSString *IPAddress) {
        NSLog(@"%@",IPAddress);
        _ip.text = [NSString stringWithFormat:@"%@:",IPAddress];
    }];
    httpServer = [[HTTPServer alloc] init];
	[httpServer setType:@"_http._tcp."];
	NSString *webPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Web"];
    [httpServer setDocumentRoot:webPath];
    [self startServer];
}

- (void)startServer
{
	NSError *error;
	if([httpServer start:&error])
	{
        NSLog(@"port:%hu",[httpServer listeningPort]);
        _port.text = @([httpServer listeningPort]).stringValue;
	}
	else
	{
        NSLog(@"error:%@",error);
	}
}

@end
